package com.autoTools.view;

import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import com.autoTools.util.Constants;
import com.autoTools.util.LogUtil;

public class TreeView extends BaseView
{
	private JTree mTree = null;
	private DefaultMutableTreeNode TreeRoot;
	private DefaultTreeModel TreeModel;
	private Map<String, DefaultMutableTreeNode> dataMap = new HashMap<String, DefaultMutableTreeNode>();

	public TreeView()
	{
		TreeRoot = creatTreeNode("root");
		TreeModel = new DefaultTreeModel(TreeRoot);// 创建一个树模型
		mTree = new JTree(TreeModel);
		mTree.setEnabled(true);
		mTree.setVisible(true);
		mTree.addTreeSelectionListener(new TreeSelectionListener()
		{

			@Override
			public void valueChanged(TreeSelectionEvent e)
			{
				// TODO Auto-generated method stub
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) mTree.getLastSelectedPathComponent();
				if (node == null)
					// Nothing is selected.
					return;
			}
		});
		mTree.addMouseListener(new MouseAdapter() {

			public void mouseReleased(java.awt.event.MouseEvent evt) {

				if(evt.isPopupTrigger()) {
					System.out.println("popup trigger"); // 输出字符串信息
					System.out.println(evt.getComponent()); // 输出组件属性
					System.out.println("" + evt.getX() + " " + evt.getY()); // 输出坐标
//					pop.show(p, evt.getX(), evt.getY()); // 显示浮动窗口
				}
			}

		});
		BaseInit(mTree);
	}

	public boolean addChild(BaseView child)
	{
		boolean state = false;
		if (child != null)
		{
			if (child.getParent() != null)
			{
				state = false;
			} else
			{
				if (child.getInstance() instanceof Component)
				{
					mTree.add((Component) child.getInstance(), child
							.getProperty().get(Constants.CONSTRAINTS));
					state = super.addChild(child);
				} else
				{
					LogUtil.sysLog(TAG,
							"addChild Error:child is not extends Component");
				}
			}
		}
		return state;
	}

	public void addItem(Object ...child)
	{
		super.addItem(child);
		DefaultMutableTreeNode parent = null;
		if (child != null)
		{
			if(child[0] != null && child[1] != null){
				parent = dataMap.get(child[0]);
				if (parent != null)
				{
					parent.add(creatTreeNode((String)child[1]));
					TreeModel.reload();
				}
			}
		}
	}

	public void removeItem(Object child)
	{
		DefaultMutableTreeNode rmNode = null;
		if (child != null)
		{
			if (child instanceof DefaultMutableTreeNode)
			{
				rmNode = (DefaultMutableTreeNode) child;
				dataMap.remove(rmNode.getUserObject());
				rmNode.removeFromParent();
			}
		}
	}

	private DefaultMutableTreeNode creatTreeNode(String name)
	{
		DefaultMutableTreeNode node = null;
		if (name != null)
		{
			node = new DefaultMutableTreeNode(name);
			dataMap.put(name, node);
		}
		return node;
	}

	public DefaultMutableTreeNode getTreeNodeFromMap(String name)
	{
		DefaultMutableTreeNode node = null;
		if (name != null)
		{
			node = dataMap.get(name);
		}
		return node;
	}

	public void setLabel(String label)
	{
		super.setLabel(label);
	}
}
